package com.hamilton.erik.pt.core.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class H2JdbcAccess {
    
	private static H2JdbcAccess instance = null;
	
	private final String DRIVER_NAME = "org.h2.Driver";
	
	private final String CONNECTION_PROTOCOL = "tcp";
	
	private final String SERVER_IP = "ec2-54-84-99-2.compute-1.amazonaws.com";
	private final String SERVER_PORT = "6060";
	
	private final String DB_NAME = "pt";
	
	private final String SA = "SA";
	private final String SA_PASSWORD = "COSC880PT2014";
	
	public static H2JdbcAccess getInstance() {
		if(instance == null) {
			instance = new H2JdbcAccess();
		}
		
		return instance;
	}
	
	private H2JdbcAccess() {}
	
	public Connection getConnection() throws SQLException {
		try {
			Class.forName(DRIVER_NAME);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String dbUrl = String.format("jdbc:h2:%s://%s:%s/%s",
										CONNECTION_PROTOCOL,
										SERVER_IP,
										SERVER_PORT,
										DB_NAME);
		
	    Connection conn = DriverManager.getConnection(dbUrl, SA, SA_PASSWORD);

	    return conn;
	}
	
	public static void main(String[] args) {
		try {
			Connection conn = H2JdbcAccess.getInstance().getConnection();
			
			System.out.println(conn.isClosed());
			
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
